const Router = require('koa-router');
const NoticeRouter = new Router();
const NoticeService = require('../service/NoticeService');
const R = require('../utils/R');
const Validators = require('../utils/validators');

NoticeRouter.prefix('/api/v1/notice');

NoticeRouter.get('/list',async (ctx, next) => {
    let {pageSize, pageNum } = ctx.request.query;
    Validators.isNull(pageSize, 'pageSize').isNull(pageNum, 'pageNum');
    let data = await NoticeService.findAll(pageSize, pageNum );
    ctx.body = R.okData(data)
})

NoticeRouter.get('/info', async (ctx, next) => {
    let { id } = ctx.request.query;
    Validators.isNull(id, 'id')
    let data = await NoticeService.findById(id);

    ctx.body = R.okData(data)
})

NoticeRouter.post('/save', async (ctx, next) => {
    let noticeObj = ctx.request.body;
    Validators.isNull(noticeObj.title, 'title').isNull(noticeObj.content, 'content').isNull(noticeObj.img, 'img')
    .isNull(noticeObj.create, 'create');
    await NoticeService.saveNotice(noticeObj);

    ctx.body = R.okMsg('添加成功');
})

NoticeRouter.post('/delete', async (ctx, next) => {
    let obj = ctx.request.body;
    Validators.isNull(obj.id, 'id')
    await NoticeService.deleteNotice(obj.id);

    ctx.body = R.okMsg('删除成功')
})

NoticeRouter.post('/update', async (ctx, next) => {
    let NoticeObj = ctx.request.body;
    Validators.isNull(noticeObj.title, 'title').isNull(noticeObj.content, 'content').isNull(noticeObj.img, 'img')
    .isNull(noticeObj.create, 'create').isNull(NoticeObj.id, 'id');
    await NoticeService.updateNotice(NoticeObj);
    ctx.body = R.okMsg('修改成功')
})

module.exports = NoticeRouter